
<!-- 
	自定义导航栏
 -->

<template>
	<view class="navbar">
		<view class="navbar-fixed" :style="'background-color:'+navcolor">
			<!-- 状态栏 -->
			<view :style="{height:statusBarHeight+'px'}"></view>
			<!-- 导航栏 -->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:navBarHeight+'px',width:windowWeith+'px'}">
				<view class="navbar-content_back-icon" @click="BackPage">
					<text class="lg text-white cuIcon-back"></text>
				</view>
				
				<view v-if="!isSearch" class="navbar-search" @click.stop="open">
					<!-- 非搜索页显示 -->
					<view class="navbar-search_icon">
						<text class="lg text-gray cuIcon-search"></text>
					</view>
					<view class="navbar-search_text">搜索</view>
				</view>
				
				<view v-else class="navbar-search">
					<!-- 搜索页显示 -->
					<view class="navbar-search_icon">
						<text class="lg text-gray cuIcon-search"></text>
					</view>
					<input class="navbar-search_text" type="text" v-model="val" placeholder="请输入您要搜索的课题." @input="inputChange"/>
				</view>
			</view>
		</view>
		<view :style="{height: statusBarHeight+navBarHeight+'px'}"></view>
	</view>
</template>

<script>
	export default {
		props: {
			value: {
				type: [String, Number],
				default: ''
			},
			navcolor: {
				type: String,
				default: ''
			},
			name: {
				type: String,
				default: ''
			},
			isSearch: {
				type: Boolean,
				default: false
			}
		},
		name:"navbar",
		data() {
			return {
				statusBarHeight: 20,
				navBarHeight: 44,
				windowWeith: 375,
				val: ''
			};
		},
		watch: {
			value(newVal) {
				this.val = newVal
			}
		},
		created() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度
			this.statusBarHeight = info.statusBarHeight
			this.windowWeith = info.windowWidth
			
			// h5 app mp-alipay
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取微信小程序胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			
			// （胶囊底部高度 - 状态栏的高度）+（胶囊顶部高度 - 状态栏内的高度）= 导航栏的高度
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)
			this.windowWeith = menuButtonInfo.left
			// #endif
		},
		methods: {
			BackPage() {
				uni.navigateBack({
					delta: 1
				});
			},
			open() {
				if(this.isSearch) {
					return
				}
				uni.navigateTo({
					url: "/pages/components/search/search?navcolor="+this.navcolor+"&name="+this.name
				})
			},
			inputChange(e) {
				const {value} = e.detail
				this.$emit('input', value)	// 传值
			}
		}
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-fixed {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			
			.navbar-content {
				padding: 0 15px;
				height: 45px;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;
				.navbar-content_back-icon {
					// margin-left: 10px;
					margin-right: 11px;
				}
				
				.navbar-search {
					display: flex;
					align-items: center;
					padding: 0 10px;
					width: 100%;
					height: 30px;
					border-radius: 30px;
					background-color: #fff;
					
					.navbar-search_icon {
						// width: 10px;
						// height: 10px;
						// border: 1px red solid;
						margin-right: 10px;
					}
					
					.navbar-search_text {
						width: 100%;
						font-size: 12px;
						color: #999;
					}
				}
				&.search {
					.navbar-search {
						border-radius: 5px;
					}
				}
			}
		}
	}
</style>
